home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / ToolManager / Source / Library / memory.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  115 lines

  1. /*
  2.  * memory.c  V3.1
  3.  *
  4.  * Memory management routines
  5.  *
  6.  * Copyright (C) 1990-98 Stefan Becker
  7.  *
  8.  * This source code is for educational purposes only. You may study it
  9.  * and copy ideas or algorithms from it for your own projects. It is
  10.  * not allowed to use any of the source codes (in full or in parts)
  11.  * in other programs. Especially it is not allowed to create variants
  12.  * of ToolManager or ToolManager-like programs from this source code.
  13.  *
  14.  */
  15.  
  16. #include "toolmanager.h"
  17.  
  18. /* Local data structures */
  19. static struct LowMemoryData {
  20.  struct Interrupt  lmd_Interrupt;
  21.  LONG              lmd_Signal;
  22.  struct Task      *lmd_Task;
  23.  ULONG             lmd_Mask;
  24. };
  25.  
  26. /* Low memory handler */
  27. __geta4 static ULONG LowMemoryHandler(__a1 struct LowMemoryData *lmd,
  28.                                       __a6 struct Library *SysBase)
  29. {
  30.  /* Send low memory signal to handler */
  31.  Signal(lmd->lmd_Task, lmd->lmd_Mask);
  32.  
  33.  /* That is all we can do */
  34.  return(MEM_ALL_DONE);
  35. }
  36.  
  37. /* Interrupt structure for low memory handler */
  38. static struct LowMemoryData LowMemoryInterrupt = {
  39.  NULL, NULL, NT_INTERRUPT, 50, ToolManagerName,
  40.  &LowMemoryInterrupt, (VOID *()) &LowMemoryHandler,
  41.  NULL, 0
  42. };
  43.  
  44. /* Install low memory handler */
  45. #define DEBUGFUNCTION StartLowMemoryWarning
  46. LONG StartLowMemoryWarning(void)
  47. {
  48.  MEMORY_LOG(LOG0(Entry))
  49.  
  50.  /* Allocate Signal for file notification */
  51.  if ((LowMemoryInterrupt.lmd_Signal = AllocSignal(-1)) != -1) {
  52.  
  53.   MEMORY_LOG(LOG1(Signal, "%ld", LowMemoryInterrupt.lmd_Signal))
  54.  
  55.   /* Initialize interrupt structure */
  56.   LowMemoryInterrupt.lmd_Task = FindTask(NULL);
  57.   LowMemoryInterrupt.lmd_Mask = (1 << LowMemoryInterrupt.lmd_Signal);
  58.  
  59.   /* Add low memory handler */
  60.   AddMemHandler((struct Interrupt *) &LowMemoryInterrupt);
  61.  }
  62.  
  63.  MEMORY_LOG(LOG1(Result, "%ld", LowMemoryInterrupt.lmd_Signal))
  64.  
  65.  return(LowMemoryInterrupt.lmd_Signal);
  66. }
  67.  
  68. /* Remove low memory handler */
  69. #undef  DEBUGFUNCTION
  70. #define DEBUGFUNCTION StopLowMemoryWarning
  71. void StopLowMemoryWarning(void)
  72. {
  73.  MEMORY_LOG(LOG1(Signal, "%ld", LowMemoryInterrupt.lmd_Signal))
  74.  
  75.  /* Remove low memory handler */
  76.  RemMemHandler((struct Interrupt *) &LowMemoryInterrupt);
  77.  
  78.  /* Free signal */
  79.  FreeSignal(LowMemoryInterrupt.lmd_Signal);
  80. }
  81.  
  82. /* Handle low memory situations */
  83. #undef  DEBUGFUNCTION
  84. #define DEBUGFUNCTION HandleLowMemory(void)
  85. void HandleLowMemory(void)
  86. {
  87.  struct MinNode *n = GetHead(GetHandleList());
  88.  
  89.  MEMORY_LOG(LOG0(Entry))
  90.  
  91.  /* Traverse handle list to purge image caches */
  92.  while (n) {
  93.   Object *obj1 = (Object *) TMHANDLE(n)
  94.                              ->tmh_ObjectLists[TMOBJTYPE_IMAGE].mlh_Head;
  95.   Object *obj2;
  96.  
  97.   MEMORY_LOG(LOG1(Handle, "0x%08lx", n))
  98.  
  99.   /* Scan image object list */
  100.   while (obj2 = NextObject(&obj1)) {
  101.  
  102.    MEMORY_LOG(LOG1(Object, "0x%08lx", obj2))
  103.  
  104.    /* Send screen open/close method to object */
  105.    DoMethod(obj2, TMM_PurgeCache);
  106.   }
  107.  
  108.   /* Get next entry in handle list */
  109.   n = GetSucc(n);
  110.  }
  111. }
  112.  
  113. /* Include global memory code */
  114. #include "/global_memory.c"
  115.